package com.zut.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zut.dto.DishDto;
import com.zut.pojo.Category;
import com.zut.pojo.Dish;
import com.zut.pojo.R;
import com.zut.service.CategoryService;
import com.zut.service.DishFlavorService;
import com.zut.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;


    /*
    * 新增菜品
    * */

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info("新增菜品：{},",dishDto);
        dishService.saveWithFlavor(dishDto);

        return R.success("新增菜品成功");
    }

    /*
    *
    * 分页查询菜品
    * 条件查询菜品
    *
    * */

    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        log.info("page = {},pageSize = {},name = {}",page,pageSize,name);

        //分页构造器
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
        List<Dish> records = pageInfo.getRecords();
        List<DishDto> list = records.stream().map((record) ->{

            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(record,dishDto);

            Long categoryId = record.getCategoryId(); //分类id

            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);


            if (category != null){

                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }


            return dishDto;

        }).collect(Collectors.toList());

        dishDtoPage.setRecords(list);


        return R.success(dishDtoPage);

    }


    /*
    *
    * 根据id查询菜品表和口味表（页面回显)
    * */

    @GetMapping("/{id}")
    public R<DishDto> select(@PathVariable Long id){
        log.info("根据id查询菜品");

        DishDto dishDto = dishService.selectWithFlavor(id);

        return R.success(dishDto);
    }


    /*
    *
    * 修改菜品信息
    * */

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        log.info("修改菜品：{},",dishDto);
        dishService.updateWithFlavor(dishDto);

        return R.success("修改菜品成功");
    }





    /*
    * 批量删除菜品
    * */

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除菜品，id为：{}",ids);
        dishService.removeByIds(ids);

        return R.success("删除成功");
    }

    /*
    *
    * 批量停售/起售
    * */

    @PostMapping("/status/{status}")
    public R<String> updatestatus(@PathVariable Integer status,@RequestParam List<Long> ids){

        log.info("停售/起售的菜品，id为，{}",ids);

        //条件构造器
        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
        //过滤条件
        updateWrapper.set(Dish::getStatus,status).in(Dish::getId,ids);

        dishService.update(updateWrapper);

        return R.success("批量起售和停售菜品成功");
    }


    /*
    *
    * 添加套餐中菜品的显示
    * */

    @GetMapping("/list")
    public R<List<Dish>> list(Dish dish){

        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //根据分类查询
        queryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        //根据名字模糊查询
        queryWrapper.like(dish.getName() != null,Dish::getName,dish.getName());
        //根据状态查询（停售不查）
        queryWrapper.eq(Dish::getStatus,1);
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(queryWrapper);

        return R.success(list);
    }



}
